/**$Licence:
 *   Use this however/wherever you like, just don't blame me if it breaks anything.
 *   Copyright (C) 2008 - 2013 xingsheng.zhu@gmail.com All rights reserved.
 */
package com.blazemina.executor;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import com.blazegame.logging.Logger;
import com.blazegame.logging.LoggerFactory;
import com.blazemina.CommandContext;
import com.blazemina.config.BlazeminaConfigManager;
import com.blazemina.config.entities.CommandConfig;
import com.blazemina.exception.BlazeminaRuntimeException;
import com.blazemina.util.ClassUtils;

/**
 * @author JackChu
 * @version 2.0.0 2013-2-21 下午4:40:15
 */
public class CommandMethodExecutor {

	private final static Logger logger = LoggerFactory
			.getLogger(CommandMethodExecutor.class);

	/**
	 * 
	 * @throws InvocationTargetException
	 * @throws IllegalAccessException
	 * @throws IllegalArgumentException
	 * @throws Exception
	 */

	public static void execute(CommandContext commandContext) {
		String command = commandContext.getMessage().getCommand();
		CommandConfig commandMapping = BlazeminaConfigManager.me()
				.getCommandMapping(command);
		if(commandMapping == null){
			logger.error("request command is not exist,command:#0 , client address:#1",command,commandContext.getSession().getRemoteAddress().toString());
			throw new BlazeminaRuntimeException("request command is not exist,command:"+command);
		}
		Method method = commandMapping.getMethod();
		ClassUtils.invokeMethod(method, commandMapping.getCommandObject(), commandContext);
	}
}
